<?php


namespace app\admin\controller;

use app\common\controller\Adminbase;
use think\Db;

class Access extends Adminbase
{
    protected function initialize()
    {
        parent::initialize();
    }

    public function index()
    {
        if ($this->request->isAjax()) {
            list($page, $limit) = $this->buildTableParames();
            $where[]=['is_del','=',0];
            $filter = input('filter');
            if ($filter) {
                $filter = json_decode($filter, true);
                if (isset($filter['id'])) {
                    $where[] = ['a.id', '=', $filter['id']];
                }
                if (isset($filter['id'])) {
                    $where[] = ['a.id', '=', $filter['id']];
                }
                if (isset($filter['create_time'])) {
                    $create_time = explode(' - ', $filter['create_time']);
                    foreach ($create_time as $k => $v) {
                        $create_time[$k] = strtotime($v);
                    }
                    $where[] = ['a.create_time', 'between', [$create_time[0], $create_time[1]]];
                }
                if (isset($filter['people'])) {
                    $where[] = ['a.people', 'like', '%' . $filter['people'] . '%'];
                }
                if (isset($filter['phone'])) {
                    $where[] = ['a.phone', 'like', '%' . $filter['phone'] . '%'];
                }
            }
            $_list = Db::name('access')
                ->alias('a')
                ->field('a.*,m.nickname,m.mobile')
                ->join('member m','a.member_id=m.id')
                ->page($page, $limit)
                ->where($where)
                ->order('create_time desc')
                ->select();
            foreach ($_list as $k=>$v){
                $_list[$k]['create_time']=date('Y-m-d H:i:s',$_list[$k]['create_time']);
            }
            $total = Db::name('access')
                ->alias('a')
                ->field('a.*,m.nickname,m.mobile')
                ->join('member m','a.member_id=m.id') ->where($where)->count();
            $result = ["code" => 0, "count" => $total, "data" => $_list];
            return json($result);
        }
        return $this->fetch();
    }
    /**
     * 状态
     */
    public function setstate()
    {
        $id = $this->request->param('id/d', 0);

        $status = $this->request->param('value/d');

        if (Db::name('access')->where('id', $id)->update(['status' => $status])) {

            $this->success('操作成功！');
        } else {
            $this->error('操作失败！');
        }
    }

    //删除
    public function del()
    {
        $ids = $this->request->param('ids/a', null);
        try {
            //软删除
            foreach ($ids as $id) {
                Db::name('access')->where('id', $id)->update([
                    'is_del'=>1
                ]);
            }
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('删除成功！');
    }

    //增加
    public function add()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $insert_data = array(
                'gbt' => $data['gbt'],
                'name' => $data['name'],
                'sort' => $data['sort'],
                'create_time'=>strtotime($data['create_time']),
                'effect_time'=>strtotime($data['effect_time']),
                'status' => $data['status'],
            );

            $stmt = Db::name('access')->insertGetId($insert_data);
            if ($stmt) {
                $this->success('操作成功！');
            } else {
                $this->error('操作失败！');
            }
        } else {
            return $this->fetch();
        }
    }

    public function edit()
    {
        if ($this->request->isPost()) {
            $data = $this->request->Post();
            $update_data = array(
                'id' => $data['id'],
                'gbt' => $data['gbt'],
                'name' => $data['name'],
                'sort' => $data['sort'],
                'create_time'=>strtotime($data['create_time']),
                'effect_time'=>strtotime($data['effect_time']),
                'status' => $data['status'],
            );
            $stmt = Db::name('access')->update($update_data);
            if ($stmt) {
                $this->success('操作成功！');
            } else {
                $this->error('操作失败！');
            }
            return $data;
        }
        $id = $this->request->param('id/d', 0);
        $data = Db::name('access')->find($id);
        $this->assign('data', $data);
        return $this->fetch();
    }

    public function editfee()
    {
        $id = $this->request->param('id/d', 0);
        if ($this->request->isPost()) {
            $data = $this->request->Post();
            $update_data = array(
                'id' => $data['id'],
                'js_money' => $data['js_money'],
                'is_js'=>1,
                'js_time'=>time()
            );
            $stmt = Db::name('access')->update($update_data);
            $query_order=Db::name('access')->find($id);

            addMoney($query_order['member_id'],$data['js_money'],2,$id,5);
            $content='评估订单消耗余额'.$data['js_money'].'元';
            sendMsg(2, '估价消耗余额', $content, $query_order['member_id']);

            if ($stmt) {
                $this->success('操作成功！');
            } else {
                $this->error('操作失败！');
            }
            return $data;
        }
        $data = Db::name('access')->find($id);
        $this->assign('data', $data);
        return $this->fetch();
    }


}